package com.view
{
	import com.ApplicationFacade;
	import com.model.UserProxy;
	import com.model.vo.UserVO;
	import com.view.components.UserList;
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;


	public class UserListMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'UserListMedistor';
		private var userProxy:UserProxy;
		public function UserListMediator(viewComponent:Object)
		{
			super(NAME,viewComponent);
			userList.addEventListener( UserList.NEW, 	onNew );
			userList.addEventListener( UserList.DELETE, onDelete);
			userList.addEventListener( UserList.SELECT, onSelect );
			
			userProxy = facade.retrieveProxy(UserProxy.NAME) as UserProxy;
			userList.users = userProxy.users;
		}
		private function get userList():UserList
		{
		    return viewComponent as UserList;
		}
		
		private function onNew( event:Event ):void
		{
			var user:UserVO = new UserVO();
			sendNotification( ApplicationFacade.NEW_USER, user );
		}
		
		private function onDelete( event:Event ):void
		{
			sendNotification( ApplicationFacade.DELETE_USER,
							  userList.selectUser );
		}
		
		private function onSelect( event:Event ):void
		{
			sendNotification( ApplicationFacade.USER_SELECTED,
							  userList.selectUser );
		}
		
		override public function listNotificationInterests():Array
		{
			return [
					ApplicationFacade.CANCEL_SELECTED,
					ApplicationFacade.USER_UPDATED
				   ];
		}
		
		override public function handleNotification( note:INotification ):void
		{
			switch ( note.getName() )
			{
				case ApplicationFacade.CANCEL_SELECTED:
					userList.deSelect();
					break;
					
				case ApplicationFacade.USER_UPDATED:
					userList.deSelect();
					break;
					
			}
		}
	}
}